<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优先级队列</title>
</head>

<body>
    <script>
        // 封装优先级队列
        function PriorityQueue() {

            // 在PriorityQueue中重新创建一个类： 可以理解为内部类
            function QueueElement(element, priority) {
                this.element = element;
                this.priority = priority;
            }


            // 封装属性
            this.items = []

            // 实现插入方法
            PriorityQueue.prototype.enqueue = function (element, priority) {
                // 1.创建QueueElement对象
                var queueElement = new QueueElement(element, priority)

                // 2. 判断队列是否为空
                if (this.items.length == 0) {
                    this.items.push(queueElement)
                } else {
                    var added = false
                    for (var i = 0; i < this.items.length; i++) {
                        if (queueElement.priority < this.items[i].priority) {
                            this.items.splice(i, 0, queueElement)
                            added = true
                            break
                        }
                    }
                    // 比前面的优先级都低，直接放到队列最后
                    if (!added) {
                        this.items.push(queueElement)
                    }
                }
            }

            // 2. dequeue() 方法从队列前端删除元素
            PriorityQueue.prototype.dequeue = function () {
                return this.items.shift()
            }
            // 3. front() 方法查看队列前端的元素
            PriorityQueue.prototype.front = function () {
                return this.items[0]
            }
            // 4. isEmpty() 方法 查看队列中是否为空
            PriorityQueue.prototype.isEmpty = function () {
                return this.items.length == 0
            }
            // 5. size() 方法 查看队列中元素的个数
            PriorityQueue.prototype.size = function () {
                return this.items.length
            }
            // 6. toString() 方法 将对列中的元素以字符串形式输出
            PriorityQueue.prototype.toString = function () {
                let resultString = '';
                // for (var i = 0; i < this.items.length; i++) {
                //     resultString += this.items[i].element + '-' + this.items[i].priority + ' '
                // }
                for (let i of this.items) {
                    resultString += i.element + '-' + i.priority + ' '
                }
                return resultString
            }
        }

        // 测试代码
        var test = new PriorityQueue()

        // enqueue方法
        test.enqueue('abc', 100)
        test.enqueue('dfg', 200)
        test.enqueue('wer', 300)
        test.enqueue('yui', 400)
        console.log(test);                   // 0: QueueElement {element: 'abc', priority: 100}
                                            // 1: QueueElement {element: 'dfg', priority: 200}
                                            // 2: QueueElement {element: 'wer', priority: 300}
                                            // 3: QueueElement {element: 'yui', priority: 400}

    </script>
</body>

</html>